package com.think.oauth.user.dao.user;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.think.oauth.user.model.Permission;

@Mapper
public interface PermissionDao extends BaseMapper<Permission>{
	@Select("select * from think_permission where permission_id in (select permission_id from think_role_permission where role_id = #{roleId})")
    Set<Permission> getRolePermissions(Integer roleId);
	
	
	@Select("SELECT think_permission.name FROM think_permission WHERE permission_id IN(SELECT think_role_permission.permission_id FROM think_role_permission WHERE role_id IN(SELECT think_user_role.role_id FROM"
			+ " think_user_role WHERE user_id=#{userId}))  AND think_permission.delflag!=1 AND think_permission.client=#{client}")
	public List<String> selectPermissionByUserId(@Param("userId") Integer userId,@Param("client")String client);
	
	
	@Select("SELECT * FROM `think_permission` WHERE ISNULL(parent) AND delflag!=1 AND client=#{client}")
	@Results({
		@Result(column = "permission_id",property = "permissionId"),
		@Result(column = "permission_id",property = "key"),
		@Result(column = "permission_id",property = "children",many = @Many(select = "com.think.oauth.user.dao.user.PermissionDao.getChildrenList",fetchType = FetchType.EAGER))
	})
	public IPage<Map<String, Object>> getRolePermissionTrees(IPage<Map<String, Object>> page,@Param("client")String client);
	
	@Select("SELECT * FROM `think_permission` WHERE parent=#{parent} AND delflag!=1")
	@Results({
		@Result(column = "permission_id",property = "permissionId"),
		@Result(column = "permission_id",property = "key"),

		@Result(column = "permission_id",property = "children",many = @Many(select = "com.think.oauth.user.dao.user.PermissionDao.getChildrenList",fetchType = FetchType.EAGER))
	})
	public List<Map<String, Object>> getChildrenList(Integer parent);
	
	
}
